/** 
 * MALL_STORAGE:
 * 
 * 记录浏览商品
 * productBrowse
 * 获取最近浏览商品列表
 * browseHistory
 * 计算浏览数量
 * countHistory
 * 清空浏览历史
 * clearHistory
 * 
 * 收藏列表
 * collectionList
 * 收藏商品(productId, productName, typeId, color, memory, picture, price)
 * collect
 * 收藏商品({productId, productName, typeId, color, memory, picture, price})
 * collectItem
 * 带提示的收藏商品
 * collectWithTips
 * 取消收藏(productId, typeId)
 * cancelCollect
 * 判断是否已经收藏(productId, typeId)
 * checkCollect
 * 计算收藏数量
 * countCollect
 * 
 * 保存临时数据(name,object)
 * tmpSave
 * 获取临时数据(name)
 * tmpGet
 */

(function($){
	$.extend($, {
		MALL_STORAGE : (function(){
			
			var userId = '123';
			
			var self = {};
			
			var storage = {
				addToArray : function(name,value,max_length){
					if(value.id) {
						if(this.contains(name,value.id)){
							this.removeFromArray(name,value.id);
						}
						var arr = this.get(name);
						if (typeof arr === 'undefined' || arr === null) {
							arr = [];
						}
						if (typeof arr.push !== 'undefined') {
							arr.push(value);
						}
						while (typeof max_length === 'number' && arr.length > max_length) {
							arr.splice(0, 1);
						}
						this.put(name, arr);
						return true;
					}else{
						console.log('need specific id');//value.id 主键
						return false;
					}
				},
				removeFromArray: function(name,id){
					var arr = this.get(name);
					if(arr && arr.length){
						var removeId = null;
						for (var i in arr) {
							if (arr[i].id === id) {
								removeId = i;
							}
						}
						if(removeId != null){
							arr.splice(removeId, 1);
							this.put(name, arr);
							return true;
						}
					}
					return false;
				},
				contains: function(name,id){
					var arr = this.get(name);
					if(arr&&arr.length){
						for (var i in arr) {
							if (typeof arr[i].id !== 'undefined' && arr[i].id === id) {
								return true;
							}
						}
					}
					return false;
				},
				put : function(name,value){
					if(typeof value !== 'string'){
						try{
							value = JSON.stringify(value);
						}catch(e){
							console.log(e);
						}
					};
					localStorage.setItem(name,value);
				},
				get : function(name){
					var value = localStorage.getItem(name);
					if(typeof value !== 'undefined') {
						try {
							return JSON.parse(value);
						}catch(e){
							return value;
						}
					}
					else {
						return null;
					}

				},
				remove : function(name){
					localStorage.removeItem(name)
				},
				clear : function(){
					localStorage.clear();
				},
				//缓存大小
				size : function(){
					var length=JSON.stringify(localStorage).length-2;
					if(length===0){
						return 0;
					}
					var dw="B";
					if(length>=1024)
					{
						length/=1024;
						dw="K";
					}
					if(length>=1024)
					{
						length/=1024;
						dw="M";
					}
					return length+dw;
				}
			};

			//浏览历史
			var history = {
				prefix: 'HISTORY_',
				//浏览历史记录
				get: function(){
					var list = storage.get(history.prefix + userId.toString()) || [];
					if(list.length){
						var now = new Date().getTime();
						for(var i in list){
							if(list.hasOwnProperty(i)){
								var timeDiff = Util.date.getTimeDiff(list[i].time, now);
								list[i].timeDiff = timeDiff;
							}
						}
					}
					return list.reverse();
				},
				//添加
				add: function(productId, productName, picture, price){
					var product = {
						id: productId,
						productId: productId,
						productName: productName,
						picture: picture,
						price: price,
						time: new Date().getTime()
					}
					return storage.addToArray(history.prefix + userId.toString(), product, 20);
				},
				clear: function(){
					return storage.remove(history.prefix + userId.toString());
				},
				count: function(){
					var list = storage.get(history.prefix + userId.toString()) || [];
					return list.length;
				}
			};
			
			//收藏
			var collection = {
				prefix: 'COLLECTION_',
				get: function(){
					var collectionList = storage.get(collection.prefix + userId.toString()) || [];
					return collectionList.reverse();
				},
				//收藏
				add: function(productId, productName, typeId, color, memory, picture, price){
					var product = {
						id: productId + '_'+ typeId,
						productId: productId,
						productName: productName,
						typeId: typeId,
						color: color,
						memory: memory,
						picture: picture,
						price: price
					}
					return storage.addToArray(collection.prefix + userId.toString(), product);
				},
				//收藏
				addItem: function(item){
					var product = {
						id: item.productId.toString() + '_'+ item.typeId.toString(),
						productId: item.productId,
						productName: item.productName,
						typeId: item.typeId,
						color: item.color,
						memory: item.memory,
						picture: item.picture,
						price: item.price
					}
					return storage.addToArray(collection.prefix + userId.toString(), product);
				},
				//取消收藏
				remove: function(productId, typeId){
					return storage.removeFromArray(collection.prefix + userId.toString(), productId + '_' + typeId);
				},
				//是否已经收藏
				check: function(productId, typeId){
					return storage.contains(collection.prefix + userId.toString(), productId + '_' + typeId);
				},
				count: function(){
					var list = storage.get(collection.prefix + userId.toString()) || [];
					return list.length;
				}
			};
			
			//sessionStorage
			var tmpStorage = {
				put : function(name,value){
					if(typeof value !== 'string'){
						try{
							value = JSON.stringify(value);
						}catch(e){
							console.log(e);
						}
					};
					sessionStorage.setItem(name,value);
				},
				get : function(name){
					var value = sessionStorage.getItem(name);
					if(typeof value !== 'undefined') {
						try {
							return JSON.parse(value);
						}catch(e){
							return value;
						}
					}
					else {
						return null;
					}
				}
			};
			
			self.productBrowse = history.add;
			self.browseHistory = history.get;
			self.clearHistory = history.clear;
			self.countHistory = history.count;
			
			self.collectionList = collection.get;
			self.collect = collection.add;
			
			self.collectWithTips = function(productId, productName, typeId, color, memory, picture, price){
				self.collect(productId, productName, typeId, color, memory, picture, price);
				Util.Tips.success('收藏成功','bottom',true);
			}
			
			self.collectItem = collection.addItem;
			self.cancelCollect = function(productId,typeId){
				collection.remove(productId,typeId);
				Util.Tips.success('取消收藏成功','bottom',true);
			}
			self.checkCollect = collection.check;
			self.countCollect = collection.count;
			
			
			self.tmpSave = tmpStorage.put;
			self.tmpGet = tmpStorage.get;
			
			return self;
		})()
	});
})(jQuery);